//
//  HGGoodsDetailVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/3.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGGoodsDetailVC.h"
#import "HomeHeaderView.h"
#import "HGGoodsHeaderView.h"
#import "HGGoodsModel.h"
#import "QQGoodsAdressVC.h"
#import "HGPlaceOderVC.h"
#import "HGSeletedView.h"
#import "HGAddressModel.h"

@interface HGGoodsDetailVC ()<HGGoodsHeaderViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) HomeHeaderView *headrView;

@property (strong, nonatomic) HGGoodsHeaderView *goodHeaderView;

@property (strong, nonatomic) HGGoodsModel *goodsModel;

@property (strong, nonatomic) HGSeletedView *seletedView;

@property (strong, nonatomic) HGAddressModel *addressModel;
@end

@implementation HGGoodsDetailVC {
    NSString *countStr;
    NSInteger userChooseType;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self initDatas];
}

- (void)initSubViews {
    
    WS(weakSelf)
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fenxiang"] style:(UIBarButtonItemStyleDone) target:self action:@selector(shareGoods)];
    
    _headrView = [[HomeHeaderView alloc] init];
    [self.scrollView addSubview:_headrView];
    [_headrView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 325));
    }];
    
    _goodHeaderView = [[HGGoodsHeaderView alloc] init];
    _goodHeaderView.delegate = self;
    [self.scrollView addSubview:_goodHeaderView];
    
    [_goodHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headrView.mas_bottom);
        make.left.right.equalTo(self.headrView);
        make.height.offset(265);
    }];
    
    
    _seletedView = [HGSeletedView shareSeletedView];
    _seletedView.callBack = ^(HGAddressModel *model) {
        weakSelf.addressModel = model;
    };
    
    _seletedView.addAddress = ^{
        [weakSelf.navigationController pushViewController:[QQGoodsAdressVC new] animated:YES];
    };
    [self.view addSubview:_seletedView];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareGoods {
    [[XNProgressHUD shared] showSuccessWithTitle:@"分享是不可能的，别做梦了 骚年"];
}

#pragma mark - 选择收货地址
- (void)chooseType:(NSInteger)type {
    _goodsModel.userChooseType = type;
    [[XNProgressHUD shared] showLoadingWithTitle:@"加载中" maskType:(XNProgressHUDMaskTypeBlack)];
    _seletedView.isFoodView = type == 2 ? YES : NO;
    _seletedView.titleLabel.text = type == 2 ? @"自提地址" : @"收货地址";
    NSString *url = type == 2 ? @"app/user/address/selflift/list" : @"app/user/address/list";
    [[LMJRequestManager sharedManager] GET:KBaseURL(url) parameters:nil completion:^(LMJBaseResponse *response) {
        [[XNProgressHUD shared] dismiss];
        if (response.responseModel.status == 1) {
            _seletedView.addressModels = [HGAddressModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
        } else {
            _seletedView.addressModels = @[];
        }
    }];
    [_seletedView begain];
}

#pragma mark - 数据
- (void)initDatas {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveCount:) name:@"cumulative" object:nil];
    
    [[LMJRequestManager sharedManager] POST:KBaseURL(@"app/product/detail") parameters:@{@"id" : [NSNumber numberWithInteger:_goodId]} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            _goodsModel = [HGGoodsModel mj_objectWithKeyValues:response.responseModel.data];
            _goodsModel.userChooseCount = 6;
            _headrView.images = response.responseModel.data[@"carouselFaceImgs"];
            _goodHeaderView.model = _goodsModel;
            
            NSArray *detailArr = response.responseModel.data[@"detailFaceImgs"];
            
            [detailArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                UIImageView *detailImageView = [[UIImageView alloc] init];
                [detailImageView sd_setImageWithURL:kPhotoURL(obj) placeholderImage:[UIImage imageNamed:@"jiazai"] options:(SDWebImageProgressiveDownload)];
                [_scrollView addSubview:detailImageView];
                
                [detailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(_goodHeaderView.mas_bottom).offset(idx * (500 * kScale));
                    make.size.mas_equalTo(CGSizeMake(kScreenWidth, 500));
                    make.left.equalTo(_goodHeaderView);
                }];
            }];

            [_scrollView setContentSize:CGSizeMake(kScreenWidth, 570 + (500 * kScale) * detailArr.count)];
        }
    }];
}

#pragma mark - 选择多少个数
- (void)receiveCount:(NSNotification *)center {
    _goodsModel.userChooseCount = [center.userInfo[@"count"] integerValue];
}

#pragma mark - 立即购买
- (void)buyGoods {
    if (_addressModel == nil) {
        [[XNProgressHUD shared] showInfoWithTitle:@"请先选择地址"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self chooseType:1];
        });
        return;
    }
    HGPlaceOderVC *vc = [HGPlaceOderVC new];
    vc.model = _goodsModel;
    vc.addressModel = _addressModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        [self.view addSubview:_scrollView];
        
        UIButton *sender = [[UIButton alloc] init];
        sender.backgroundColor = kColorMainTheme;
        [sender setTitle:@"立即购买" forState:(UIControlStateNormal)];
        [sender addTarget:self action:@selector(buyGoods) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:sender];
        
        [sender mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.right.left.equalTo(self.view);
            make.height.offset(48 + kTabbarSafeBottomMargin);
        }];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.bottom.equalTo(sender.mas_top);
        }];
    }
    return _scrollView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (BOOL)shouldShowShadowImage {
    return NO;
}

- (UIImage *)navigationBarBackgroundImage {
    return [UIImage new];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"receiveCount" object:nil];
}

@end
